package config

import (
	"fmt"
	"github.com/surrealdb/surrealdb.go"
)

var DB *surrealdb.DB

func InitDB() error {
	var err error
	DB, err = surrealdb.New("ws://localhost:8000/rpc")
	if err != nil {
		return fmt.Errorf("failed to connect to database: %v", err)
	}

	if _, err = DB.Use("demo", "students"); err != nil {
		return fmt.Errorf("failed to use namespace/database: %v", err)
	}

	authData := &surrealdb.Auth{
		Username: "root",
		Password: "root",
	}

	if _, err = DB.SignIn(authData); err != nil {
		return fmt.Errorf("failed to sign in: %v", err)
	}

	return nil
}
